package com.example.finalexam;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;

public class InputActivity extends AppCompatActivity {
    Button buttoninput,buttonre,buttonquery,buttondelete;
    private static final String TAG ="InputActivity" ;
    private DBManager mgr;
    public String res;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        buttoninput = findViewById(R.id.button_input);
        buttonre = findViewById(R.id.button_re);
        buttonquery = findViewById(R.id.buttonquery);
        buttondelete = findViewById(R.id.buttondelete);
        mgr=new DBManager(this);
        }
    //点击录入按钮，将文件中的数据输入到数据库中
    public void InputClick(View view) {
        ArrayList<Word> words=new ArrayList<Word>();
        String fileName = "mword.txt"; //单词文件放在asset文件下
        try{
            InputStream in = getResources().getAssets().open(fileName);
            int length = in.available();
            byte [] buffer = new byte[length];
            in.read(buffer);
            in.close();
            res=new String(buffer,"utf-8");
        }catch(Exception e){
            e.printStackTrace();
        }
        //将res分割成一个String数组，再将数据传到数据库中
        Log.i(TAG,"word"+res);
        String []tmp=res.split("\n");
        Log.i(TAG,"word"+tmp[1]);
        for(int i=0;i<tmp.length;i++) {
            String []tmp_word=tmp[i].split(" ");
            Word word=new Word(tmp_word[0],tmp_word[1],0);
            words.add(word);
        }
        mgr.add(words);
        Toast.makeText(this, "" +
                "录入成功！", Toast.LENGTH_SHORT).show();
    }
    //查看所有录入单词
    @SuppressWarnings("deprecation")
    public void queryTheCursor(View view) {
        Cursor c = mgr.queryTheCursor();
        startManagingCursor(c);
        CursorWrapper cursorWrapper = new CursorWrapper(c) {
            @Override
            public String getString(int columnIndex) {
                if (getColumnName(columnIndex).equals("en_meaning")) {
                    String cn_meaning = getString(getColumnIndex("cn_meaning"));
                    return  super.getString(columnIndex)+"   "+cn_meaning;
                }
                return super.getString(columnIndex);
            }
        };
        //listview显示单词
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2,
                cursorWrapper, new String[]{"en_meaning","repeating_number"}, new int[]{android.R.id.text1, android.R.id.text2});
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
    //清除所有数据库中的数据，方便背完单词后下一次数据的录入
    public void delete(View view){
        mgr.deleteAll();
        Toast.makeText(this,""+"清除已录入数据成功，请录入新数据！",Toast.LENGTH_LONG).show();
    }
    //返回主页面
    public void reClick(View view){
        Intent intent=new Intent(InputActivity.this,MainActivity.class);
        startActivity(intent);
    }
}